//
//  NSData+Hash.m
//  ITC_iOS
//
//  Created by Benz.L on 27/08/2018.
//  Copyright © 2018 Benz.L. All rights reserved.
//

#import "NSData+Hash.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSData (Hash)

+ (NSString *)AES256EncryptText:(NSString *)plain key:(NSString *)key {
    NSData *data = [plain dataUsingEncoding:NSUTF8StringEncoding];
    data = [data AES256EncryptWithKey:key];
    return [data base64EncodedStringWithOptions:0];
}
+ (NSString *)AES256DecryptText:(NSString *)plain key:(NSString *)key {
    NSData *data = [plain dataUsingEncoding:NSUTF8StringEncoding];
    data = [data AES256DecryptWithKey:key];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv {
    return [self AES128:kCCEncrypt Key:key iv:iv];
}
- (NSData *)AES128DecryptWithKey:(NSString *)key iv:(NSString *)iv {
    return [self AES128:kCCDecrypt Key:key iv:iv];
}
- (NSData *)AES128:(CCOperation)operation  Key:(NSString *)key iv:(NSString *)iv {
    //NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];//待加密字符转为NSData型
    char keyPtr[kCCKeySizeAES128+1];
    char ivPtr[kCCKeySizeAES128+1];
    memset(keyPtr,0,sizeof(keyPtr));
    memset(ivPtr, 0, sizeof(ivPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [self length];
    size_t bufferSize = dataLength +kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesCrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,           //  加密    kCCDecrypt解密
                                          kCCAlgorithmAES128,   //填充方式
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,//工作模式
                                          keyPtr,               //AES的密钥长度有128字节、192字节、256字节几种，这里举出可能存在的最大长度
                                          kCCBlockSizeAES128,   //密文长度+补位长度
                                          ivPtr,                //偏移量，由于是对称加密，用不到
                                          [self bytes],         //字节大小
                                          dataLength,           //字节长度
                                          buffer,
                                          bufferSize,
                                          &numBytesCrypted);
    
    NSData *resultData = nil;
    if (cryptStatus ==kCCSuccess) {
        resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
    }
    free(buffer);
    return resultData;
}

- (NSData *)AES256EncryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [self length];
    
    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    NSData *result = nil;
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        result = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer); //free the buffer;
    return result;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [self length];
    
    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);
    NSData *result = nil;
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        result = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    
    free(buffer); //free the buffer;
    return result;
}

- (NSString *)base64Encode {
    NSData *base64Data = [self base64EncodedDataWithOptions:0];
    NSString *baseString = [[NSString alloc]initWithData:base64Data encoding:NSUTF8StringEncoding];
    return baseString;
}
- (NSString *)base64Decode {
    NSString *string = [[NSString alloc]initWithData:self encoding:NSUTF8StringEncoding];
    return string;
}

static char encodingTable[64] = {
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };

- (NSString *)base64Encoded {
    return [self base64EncodedWithlineLength:0];
}

- (NSString *)base64EncodedWithlineLength:(NSUInteger) lineLength {
    const unsigned char *bytes = [self bytes];
    NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
    unsigned long ixtext = 0;
    unsigned long lentext = [self length];
    long ctremaining = 0;
    unsigned char inbuf[3], outbuf[4];
    unsigned short i = 0;
    unsigned short charsonline = 0, ctcopy = 0;
    unsigned long ix = 0;
    
    while( YES ) {
        ctremaining = lentext - ixtext;
        if( ctremaining <= 0 ) break;
        
        for( i = 0; i < 3; i++ ) {
            ix = ixtext + i;
            if( ix < lentext ) inbuf[i] = bytes[ix];
            else inbuf [i] = 0;
        }
        
        outbuf [0] = (inbuf [0] & 0xFC) >> 2;
        outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
        outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
        outbuf [3] = inbuf [2] & 0x3F;
        ctcopy = 4;
        
        switch( ctremaining ) {
            case 1:
                ctcopy = 2;
                break;
            case 2:
                ctcopy = 3;
                break;
        }
        
        for( i = 0; i < ctcopy; i++ )
            [result appendFormat:@"%c", encodingTable[outbuf[i]]];
        
        for( i = ctcopy; i < 4; i++ )
            [result appendString:@"="];
        
        ixtext += 3;
        charsonline += 4;
        
        if( lineLength > 0 ) {
            if( charsonline >= lineLength ) {
                charsonline = 0;
                [result appendString:@"\n"];
            }
        }
    }
    
    return [NSString stringWithString:result];
}

@end
